<?php

class CourselistsController extends AppController {

	var $name = 'Courselists';
	var $uses = array('User', 'Courselist', 'Application', 'Course');
	
	function beforeFilter() {
		parent::beforeFilter();
		if (isset($this->passedArgs[0]) && $this->Auth->user('group_id') == 2) {
			$model = ($this->params['action'] == 'deleteCourse')? 'Course': 'Courselist';
  		$aco = $this->Acl->Aco->findByModelAndForeignKey($model, $this->passedArgs[0]);
    	$aro = $this->Acl->Aro->findByModelAndForeignKey('User', $this->Auth->user('id'));
    	if (!$this->Acl->check($aro['Aro'], $aco['Aco'])) {
      	$this->Session->setFlash($this->Auth->authError, 'flash_failure');
        $this->redirect($this->referer());
    	}
		}
	}
	
	/**
	* Default function for application.
	*/
	function edit ($id=null) {		
		
		if (empty($this->data) && !$id) {
			$this->Session->setFlash(__('No valid courselist id: Please contact gradsec@psych.uoregon.edu', true));
			$this->redirect($this->referer());
		}
		
		if (!empty($this->data)) {		
			if($this->Courselist->save($this->data)) {
				$this->Session->setFlash("Successfully updated GPA information", 'flash_success');
			} else {
				$this->Session->setFlash(__('Unble to save GPA information. Please check your responses below', true), 'flash_failure');
			}
		}
		if(empty($this->data))  
			$this->data = $this->Courselist->read(null, $id);
			
		if(!isset($this->data['Course'])) {
			$courses = $this->Course->findAllByCourselistId($id, null, null, null, 0);
			foreach ($courses as $index => $val) {
				$courses[$index] = $val['Course'];
			}
			$this->data['Course'] = $courses;
		}
		$this->set('app_id', $this->Courselist->field('application_id')); 		
		$this->set(compact('data'));
		$this->set('info', $this->_getContentMsg('courselist_index_info'));
		
	} //end index
	
	
	/**
	*add a course to this courselist
	*/
	function addCourse ($cl_id=null) {
		
		if (!empty($this->data)) {
			$this->Course->create();
			$this->data['Course']['courselist_id'] = $cl_id;
			
			if($this->Course->save($this->data)) {
				$this->Courselist->id = $cl_id;
				$this->Courselist->updateStatus();
				$this->Session->setFlash("Successfully Updated Course List", 'flash_success', null, 'cl');
			} else {
				$this->Session->setFlash(
					__('Unable to save course. Make sure you entered a coursename and grade.', true), 
					'flash_failure', null, 'cl');
			}
		}
		
		$this->set('data', $this->Course->findAllByCourselistId($cl_id, null, null, 0));
		
		if ($this->RequestHandler->isAjax()) {
			$this->render('courselist', 'ajax');
		} else {
		  $this->redirect(array('action'=>'edit', $cl_id));
		}
	}
	
	/**
	*Delete a course.
	*/
	function deleteCourse ($id=null) {
		
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for course', true), 'flash_failure');
			$this->redirect($this->referer());
		}
		
		$this->Course->id = $id;
		$cl_id = $this->Course->field('courselist_id');
		
		if($this->Course->delete()) {
			$this->Courselist->id = $cl_id;
			$this->Courselist->updateStatus();
		} else {
			$this->Session->setFlash(__('Error encountered, Unable to delete course.', true), 'flash_failure', null, 'cl');
		}
		
		$this->set('data', $this->Course->findAllByCourselist_id($cl_id, null, null, 0));
		
		if ($this->RequestHandler->isAjax()) {
			$this->render('courselist', 'ajax');
		} else {
		  $this->redirect(array('action'=>'edit', $cl_id));
		}
		
	}
	
}

